Page({
  data: {
    mode: 'date',  // 'date' 或 'month'
    selectedYear: 0,
    selectedMonth: 0,
    selectedDay: 0,
    weeks: ['日', '一', '二', '三', '四', '五', '六'],
    days: [],      // 当月日期数组
    prevMonthDays: [], // 上月显示的日期
    nextMonthDays: []  // 下月显示的日期
  },

  onLoad(options) {
    const { mode, year, month, day } = options;
    
    // 设置初始数据
    this.setData({
      mode,
      selectedYear: parseInt(year),
      selectedMonth: parseInt(month),
      selectedDay: parseInt(day || 1)
    }, () => {
      this.generateCalendar();
    });
  },

  // 生成日历数据
  generateCalendar() {
    const { selectedYear, selectedMonth } = this.data;
    
    // 获取当月第一天是星期几
    const firstDay = new Date(selectedYear, selectedMonth - 1, 1).getDay();
    
    // 获取当月天数
    const daysInMonth = new Date(selectedYear, selectedMonth, 0).getDate();
    
    // 获取上月天数
    const daysInPrevMonth = new Date(selectedYear, selectedMonth - 1, 0).getDate();
    
    // 获取当前日期，用于限制选择
    const now = new Date();
    const currentYear = now.getFullYear();
    const currentMonth = now.getMonth() + 1;
    const currentDay = now.getDate();
    
    // 生成当月日期数组
    const days = Array.from({length: daysInMonth}, (_, i) => {
      const day = i + 1;
      // 检查日期是否超过今天
      const isFuture = (selectedYear > currentYear) || 
                      (selectedYear === currentYear && selectedMonth > currentMonth) ||
                      (selectedYear === currentYear && selectedMonth === currentMonth && day > currentDay);
      
      return {
        day: day,
        current: true,
        disabled: isFuture
      };
    });
    
    // 生成上月显示的日期
    const prevMonthDays = Array.from({length: firstDay}, (_, i) => ({
      day: daysInPrevMonth - firstDay + i + 1,
      current: false,
      disabled: true
    }));
    
    // 计算需要显示的下月天数
    const remainingDays = (7 - ((firstDay + daysInMonth) % 7)) % 7;
    
    // 生成下月显示的日期
    const nextMonthDays = Array.from({length: remainingDays}, (_, i) => ({
      day: i + 1,
      current: false,
      disabled: true
    }));
    
    this.setData({
      days,
      prevMonthDays,
      nextMonthDays
    });
  },

  // 切换月份
  changeMonth(e) {
    const { type } = e.currentTarget.dataset;
    let { selectedYear, selectedMonth } = this.data;
    
    // 获取当前日期，用于限制选择
    const now = new Date();
    const currentYear = now.getFullYear();
    const currentMonth = now.getMonth() + 1;
    
    if (type === 'prev') {
      if (selectedMonth === 1) {
        selectedYear--;
        selectedMonth = 12;
      } else {
        selectedMonth--;
      }
    } else {
      // 限制不能选择未来的月份
      if (selectedYear === currentYear && selectedMonth === currentMonth) {
        // 已经是当前月份，不能再往后选
        return;
      }
      
      if (selectedMonth === 12) {
        selectedYear++;
        selectedMonth = 1;
      } else {
        selectedMonth++;
      }
      
      // 检查是否超过当前月份
      if (selectedYear > currentYear || (selectedYear === currentYear && selectedMonth > currentMonth)) {
        // 回退到当前月份
        selectedYear = currentYear;
        selectedMonth = currentMonth;
      }
    }
    
    this.setData({
      selectedYear,
      selectedMonth
    }, () => {
      this.generateCalendar();
    });
  },

  // 选择日期
  selectDate(e) {
    const { day, current, disabled } = e.currentTarget.dataset;
    if (!current || disabled) return;  // 不允许选择非当月日期或禁用日期
    
    this.setData({
      selectedDay: day
    });
  },

  // 确认选择
  confirmSelection() {
    const pages = getCurrentPages();
    const prevPage = pages[pages.length - 2];
    
    prevPage.handleDateSelected({
      detail: {
        year: this.data.selectedYear,
        month: this.data.selectedMonth,
        day: this.data.mode === 'date' ? this.data.selectedDay : 1
      }
    });
    
    wx.navigateBack();
  },

  // 取消选择
  cancelSelection() {
    wx.navigateBack();
  }
}); 